package com.googlecode.cswish.struts.conversion;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.googlecode.cswish.model.PageElement;

/**
 * Convert string object, use listener to process them
 * 
 * @author feng Date: 2009-5-5
 *
 */
public class StringElementConverter implements ElementConverter {

	@Resource
	private SelectElementConverter selectElementConverter;
	@Resource
	private HiddenElementConverter hiddenElementConverter;
	@Resource
	private PasswordElementConverter passwordElementConverter;
	@Resource
	private TextAreaElementConverter textAreaElementConverter;
	@Resource
	private TextFieldElementConverter textFieldElementConverter;
	
	private List<ElementConverter> convertList;
	
	@PostConstruct
	public void registerDefault() {
		convertList = new ArrayList<ElementConverter>();
		convertList.add(selectElementConverter);
		convertList.add(hiddenElementConverter);
		convertList.add(passwordElementConverter);
		convertList.add(textAreaElementConverter);
		convertList.add(textFieldElementConverter);
	}
	
	public List<ElementConverter> getConvertList() {
		return convertList;
	}

	@Override
	public PageElement convert(ConversionContext context) {
		// Find the match converter to convert the string object 
		for (ElementConverter converter : convertList) {
			PageElement pageElement = converter.convert(context);
			
			if (pageElement != null) {
				return pageElement;
			}
		}
		return textFieldElementConverter.convert(context);
	}
}
